import 'dart:convert';
import 'dart:io';
import 'dart:io' as prefix1;

import 'package:flutter/material.dart' as prefix0;
import 'package:html/dom.dart';
import 'package:flutter/material.dart';
import 'package:oktoast/oktoast.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:connectivity/connectivity.dart';
import 'package:html/parser.dart';
import 'package:dio/dio.dart';
import 'package:dio_cookie_manager/dio_cookie_manager.dart';
import 'package:cookie_jar/cookie_jar.dart';

final TextEditingController _accountController = new TextEditingController();
final TextEditingController _passwordController = new TextEditingController();

class SignOut extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return OKToast(
      child: MaterialApp(
        title: 'LogOut',
        home: Scaffold(
          appBar: AppBar(
            title: prefix0.Text(
              'LogOut',
            ),
          ),
          body: Container(
            child: Column(
              children: <Widget>[
                new UserNameInput(),
                new PasswordInput(),
                new Login(),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

//用户名
class UserNameInput extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Container(
        child: new Row(
      children: <Widget>[
        new Container(
          child: prefix0.Text(
            '账户',
            style: TextStyle(
              fontSize: 24.0,
            ),
          ),
          padding: const EdgeInsets.all(20.0),
        ),
        new Container(
          width: 200.0,
          child: TextField(
            controller: _accountController,
            decoration:
                InputDecoration(hintText: '请输入您的账户', labelText: 'Account'),
            style: TextStyle(fontSize: 16.0),
          ),
        )
      ],
    ));
  }
}

//密码
class PasswordInput extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Container(
        child: new Row(
      children: <Widget>[
        new Container(
          child: prefix0.Text(
            '密码',
            style: TextStyle(
              fontSize: 24.0,
            ),
          ),
          padding: const EdgeInsets.all(20.0),
        ),
        new Container(
          width: 200.0,
          child: TextField(
            controller: _passwordController,
            obscureText: true,
            decoration:
                InputDecoration(hintText: '请输入您的密码', labelText: 'Password'),
            style: TextStyle(fontSize: 16.0),
          ),
        )
      ],
    ));
  }
}

class Login extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Container(
      child: RaisedButton(
        onPressed: () {
          save();
          selfServeLoginMethod();
        },
        child: prefix0.Text(
          '登录',
        ),
      ),
    );
  }

  var account = _accountController.text;
  var password = _passwordController.text;

//sharePreferences方法保存到本地
  void save() async {
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    sharedPreferences.setString('selfServeAccount', account);
    sharedPreferences.setString('selfServePassword', password);
  }
}

void selfServeLoginMethod() async {

  var url = "http://202.119.207.100:8080/Self/login/?302=LI";

  List<prefix1.Cookie> cookies = [new Cookie("name", "hello"), new Cookie("sing", "song")];
  CookieJar cookieJar = new CookieJar();
  cookieJar.saveFromResponse(Uri.parse("www.baidu.com"), cookies);
  List<prefix1.Cookie> results = cookieJar.loadForRequest(Uri.parse("www.baidu.com"));
  print(results);


  Response response = await Dio().get(url);
//  print(response.data is String);//data为String类型
  String responseData = response.data;
  print(responseData.indexOf('checkcode'));//checkCode字符位置为2920,具体checkCode的位置从2938开始，但是checkCode长度不固定,目测最长为4位
  print(responseData.substring(2920,2943));
  var checkCode = '';
  for(int i = 0; i < 4; i++) {
    var tempCode = responseData.substring(2938 + i, 2938 + i + 1);
    if(tempCode != "\""){
      checkCode = checkCode + tempCode;
    }
  }
  print('checkCode: ' + checkCode);
  FormData formData = new FormData.fromMap({
    "foo": "",
    "bar": "zhou123456",
    "account": "20175543",
    "checkcode": checkCode,
    "password": "952d9afce978a5c2f2883395af3ac98e",
    "code": ""
  });
//  response = await Dio().post("http://202.119.207.100:8080/Self/login/verify", data: formData);





//  var connectivityResult = await (Connectivity().checkConnectivity());
//  if (connectivityResult == ConnectivityResult.mobile) {
//    showToast("请连接校园网WIFI！");
//  } else {
//    var wifiName = await (Connectivity().getWifiName());
//    var wifiBBISD = await (Connectivity().getWifiBSSID());
//    var wifiIP = await (Connectivity().getWifiIP());
//    print(wifiName);
//    print(wifiBBISD);
//    print(wifiIP);
//    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
//    var account = await sharedPreferences.get('selfServeAccount');
//    var password = await sharedPreferences.get('slefServePassword');
//    if (account == null ||
//        password == null ||
//        account == '' ||
//        password == '') {
//      showToast('请先输入账户信息！');
//    }
//  }
}
